package com.bkhc.business.repository;

import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.OrderItemVo;
import com.bkhc.common.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OrderRepository {
    @Autowired
    private RestTemplate restTemplate;

    public PageUtil getOrders(OrderVo vo, int pageIndex, int pageSize){
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/list?pageIndex="+pageIndex+"&pageSize="+pageSize;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        PageUtil result = restTemplate.postForObject(url,vo, PageUtil.class);
        return result;
    }

    public List<OrderItemVo> queryOrderItem(String orderCode) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/item?orderCode="+orderCode;
        ResultJson<List<OrderItemVo>> result = restTemplate.postForObject(url, null, ResultJson.class);
        return result.getData();
    }

    public PageUtil getProductorOrders(OrderVo order, List<String> productCodes, int pageNum, int pageSize) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/list/productor?" +
                "pageIndex="+pageNum
                +"&pageSize="+pageSize;
        Map<String,Object> params = new HashMap<>();
        params.put("productCodes",productCodes);
        params.put("userId",order.getUserId());
        params.put("username",order.getUsername());
        params.put("receiver",order.getReceiver());
        params.put("brandCode",order.getBrand());
        params.put("brandName",order.getBrandName());
        params.put("productName",order.getProductName());
        params.put("orderCode",order.getOrderCode());
        params.put("orderStatus",order.getOrderStatus());
        HttpEntity<Object> enetity = CommonUtil.buildParam(params);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
//        PageUtil result = restTemplate.postForObject(url,productCodes, PageUtil.class);
        return result;
    }

    public void updateOrderStatus(String orderCode,String orderStatus) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/"+orderCode+"/updateOrderStatus/"+orderStatus;

        HttpEntity<Object> enetity = CommonUtil.buildParam(null);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
    }

    public void updateOrderAuditStatus(String orderCode,int orderAuditStatus) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/"+orderCode+"/updateOrderAuditStatus/"+orderAuditStatus;

        HttpEntity<Object> enetity = CommonUtil.buildParam(null);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
    }

    public PageUtil getOrdersByBussiness(OrderVo orderVo, int pageNum, int pageSize) {
        String serviceName = "ORDER-APP";
        String url = "http://" + serviceName + "/order/order/list/business?" +
                "pageIndex="+pageNum
                +"&pageSize="+pageSize;
        Map<String,Object> params = new HashMap<>();
        params.put("userId",orderVo.getUserId());
        params.put("username",orderVo.getUsername());
        params.put("receiver",orderVo.getReceiver());
        params.put("brandCode",orderVo.getBrand());
        params.put("brandName",orderVo.getBrandName());
        params.put("productName",orderVo.getProductName());
        params.put("productCode",orderVo.getProductCode());
        params.put("orderStatus",orderVo.getOrderStatus());
        params.put("group",orderVo.getOrderStatus());
        params.put("businessGroup",orderVo.getBusinessGroup());
        params.put("businessGroupMember",orderVo.getBusinessGroupMember());
        HttpEntity<Object> enetity = CommonUtil.buildParam(params);
        PageUtil result = restTemplate.postForObject(url.toString(), enetity, PageUtil.class);
        return  result;
    }
}
